<?php


namespace app\api\controller\v9;


use app\api\controller\v2\Auth;
use app\common\model\Divination;
use app\common\model\DivinationNote as DivinationNoteModel;
use think\Cache;
use think\Db;

class DivinationNote extends Auth
{
    /**
     * @title 注释列表
     * @url /note/divination
     * @desc 若无注释，则返回的json中data不存在<br/>http://localapi.goddessxzns.com/note/divination
     * @readme /doc/md/divination/note/index.md
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $uid = $this->user['id'];

        $did = input('did/d', 0);
        $page = input('page/d', 1);

        if (!$uid)
        {
            return show(config('code.error'), '用户不存在');
        }

        if (!$did)
        {
            return show(config('code.error'), '提问不存在');
        }

        $Divination = new Divination();
        // 获取提问详情
        $dice = $Divination->getDetail(['id' => $did]);
        // 若未查询到数据，则返回错误信息
        if ($dice === false || $dice['is_del'] == 1 || $dice['uid'] != $uid)
        {
            return show(config('code.error'), '数据不存在');
        }

        $is_use_cache = 1;
        if ($is_use_cache)
        {
            $data = (new DivinationNoteModel())->getListAll(['did' => $did, 'uid' => $uid, 'page' => $page], 1);
        } else
        {
            $data = Divination::field('id,did,content,create_time')->where(['did' => $did, 'uid' => $uid])->order('create_time DESC')->select();
        }


        return show(config('code.success'), '成功', $data);
    }

    /**
     * @title 添加注释
     * @desc http://localapi.goddessxzns.com/note/divination
     * @readme /doc/md/divination/note/save.md
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save()
    {
        $data['access_token'] = input('access_token/s');

        $data['content'] = htmlspecialchars(trim(input('content')));

        $data['did'] = input('did/d', 0);

        $data['uid'] = $this->user['id'];

        $validate = validate('DivinationNote');

        if ($validate->check($data) !== true)
        {
            return show(config('code.error'), $validate->getError());
        }

        $Divination = new Divination();
        // 获取提问详情
        $dice = $Divination->getDetail(['id' => $data['did']]);
        // 若未查询到数据，则返回错误信息
        if ($dice === false || $dice['is_del'] == 1 || $dice['uid'] != $data['uid'])
        {
            return show(config('code.error'), '数据不存在');
        }

        unset($data['access_token']);
        $time = time();
        $data['create_time'] = $time;
        $data['last_time'] = $time;

        if ($data['id'] = (int)(Db::table('wechat_backup_07_18_14_43_00.app_divination_note')->insertGetId($data)))
        {
            unset($data['uid']);
            unset($data['last_time']);

            Cache::rm(DivinationNoteModel::REDIS_KEY_IDS . $data['did']);

            $data['create_time'] = date('Y.m.d H:i', $data['create_time']);
            return show(config('code.success'), '成功', $data);
        }
        return show(config('code.error'), '添加失败');
    }
}